package com.situ.mall.controller.back;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Category;
import com.situ.mall.entity.CategoryCount;
import com.situ.mall.entity.Product;
import com.situ.mall.entity.Result;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.service.IProductService;

@Controller
@RequestMapping("/category")
public class CategoryController {
	@Autowired
	private ICategoryService categoryService;
	@Autowired
	private IProductService productService;
	
	@RequestMapping("/selectSecondCategoryByTopCategoryId.action")
	@ResponseBody
	public Result selectSecondCategoryByTopCategoryId(Integer topCategoryId) {
		List<Category> secondCategoryList = categoryService.selectSecondCategoryByTopCategoryId(topCategoryId);
		if (secondCategoryList == null || secondCategoryList.size() == 0) {
			return Result.error("二级分类数据查找失败");
		} else {
			return Result.success("", secondCategoryList);
		}
	}
	
	@RequestMapping("/getCategoryCountPage.action")
	public String getCategoryCountPage() {
		return "category_count";
	}
	
	@RequestMapping("/getCategoryListPage.action")
	public String getCategoryListPage() {
		return "category_list";
	}
	
	@RequestMapping("/getCategoryCount.action")
	@ResponseBody
	public Result getCategoryCount() {
		System.out.println("CategoryController.getCategoryCount()");
		List<CategoryCount> list = categoryService.getCategoryCount();
		return Result.success("", list);
	}
	
	@RequestMapping("/getCateoryInsertPage.action")
	public String getCateoryInsertPage(Model model) {
		List<Category> topCategoryList = categoryService.selectTopCategoryList();
		model.addAttribute("topCategoryList", topCategoryList);
		return "category_insert";
	}

	@RequestMapping("/getUpdateCategory.action")
	public String getUpdateCategory(Model model,Integer cateId) {
		System.out.println("CategoryController.getUpdateCategory()");
		Category category = categoryService.getUpdateCategory(cateId);
		model.addAttribute("category", category);
		return "category_update";
	}
	
	@RequestMapping("/selectByPage.action")
	@ResponseBody
	public Result selectByPage(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "5")Integer limit) {
		System.out.println("ProductController.selectByPage()");
		Result result = categoryService.selectByPage(page, limit);
		return result;
	}
	
	@RequestMapping("/deleteByPrimaryKey.action")
	@ResponseBody
	public Result deleteByPrimaryKey(int id) {
		boolean isSuccess = categoryService.deleteByPrimaryKey(id);
		if (isSuccess) {
			return Result.success("删除成功");
		} else {
			return Result.error("删除失败");
		}
	}

	@RequestMapping("/deleteAll.action")
	@ResponseBody
	public Result deleteAll(/* String ids */ Integer[] ids) {
		// System.out.println("ids: " + ids);
		// String[] array = ids.split(",");
		System.out.println(Arrays.toString(ids));// [26, 27]
		boolean isSuccess = categoryService.deleteAll(ids);
		if (isSuccess) {
			return Result.success("删除成功");
		} else {
			return Result.error("删除失败");
		}
	}
	
	@RequestMapping("/insert.action")
	@ResponseBody
	public Result insert(Category category) {
		System.out.println("CategoryController.insert()");
		System.out.println(category);
		if (category.getParentId() == null) {
			category.setParentId(0);
		}
		boolean isSuccess = categoryService.insert(category);
		if (isSuccess) {
			return Result.success("插入成功");
		} else {
			return Result.error("插入失败");
		}
	}
	
	@RequestMapping("/update.action")
	@ResponseBody
	public Result update(Category category) {
		System.out.println(category.getId());
		boolean isSuccess = categoryService.update(category);
		if (isSuccess) {
			return Result.success("修改成功");
		} else {
			return Result.error("修改失败");
		}
	}

	
}
